/******************************************************************************************************
 * Author		:	Chester Ragel
 * Date			:	01 - March - 2007
 * Description	:	A class to contain menu items.	
 * 
 * ****************************************************************************************************/

/*
 *  Context Menu Extention is the legal property of its developer
 *  Chester Ragel <chesterr@gmail.com> - Developer and Maintainer
 *	Sourceforge ID : chesterr
 *	Yahoo ID : chesterr1979 
 *	MSN : chesterr79@hotmail.com
 *	Gmail: chesterr@gmail.com
 *
 * If you want more information, found any bugs or have any doubts you 
 * can mail me or contact me through my yahoo id.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#region Namespace

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;

#endregion

namespace Utils.ShellExtension.ContextMenu
{
	/// <summary>
	/// MenuItems class is a collection, which can act as a menu as well a container for menus.
	/// </summary>
	public class MenuItems : CollectionBase
	{

		#region Private Methods

		private string sText;
		private bool bSeperator;
		private int iLocation;
		private Bitmap imgIcon;
		private bool isIconTransparent;
		private Win32.HMenu container;
		private MenuItems parent;

		#endregion 

		#region Internal properties
		
		internal Win32.HMenu Container
		{
			get
			{
				return container;
			}
			set
			{
				container = value;
			}
		}

		internal MenuItems Parent
		{
			get
			{
				return parent;
			}
			set
			{
				parent = value;
			}
		}
		
		#endregion

		#region Private Metnods

		private void IsLocationExist(int iLoc, MenuItems mItems, ref bool isExist)
		{
			foreach ( MenuItems mI in mItems )
			{
				if ( mI.Location == iLoc )
				{
					isExist = true;
				}
				if ( mI.Count > 0 )
				{
					IsLocationExist(iLoc, mI, ref isExist);
				}
			}
		}

		#endregion

		#region Properties

		/// <summary>
		/// Is the icon transparent?
		/// </summary>
		public bool IsIconTransparent
		{
			get
			{
				return isIconTransparent;
			}
			set
			{
				isIconTransparent = value;
			}
		}

		/// <summary>
		/// Text of the context menu, which will be displayed
		/// </summary>
		public string Text
		{
			get
			{
				return sText;
			}
			set
			{
				sText = value;
			}
		}

		/// <summary>
		/// If true, this context menu will behave as a seperator
		/// </summary>
		public bool IsSeperator
		{
			get
			{
				return bSeperator;
			}
			set
			{
				bSeperator = value;
			}
		}

		/// <summary>
		/// The position of the context menu. One perticular number cannot be repeated.
		/// </summary>
		public int Location
		{
			get
			{
				return iLocation;
			}
			set
			{
				bool isExist = false;
				IsLocationExist(value,this,ref isExist);
				if ( !isExist )
				{
					iLocation = value;
				}
				else
				{
					MessageBox.Show("MenuItem","Current Location exist, please select another location.",MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}

		/// <summary>
		/// Icon to show with the context menu
		/// </summary>
		public Bitmap Icon
		{
			get
			{
				return imgIcon;
			}
			set
			{
				imgIcon = value;
			}
		}

		#endregion

		#region Public Collection Methods

		/// <summary>
		/// Add menu
		/// </summary>
		/// <param name="menus"></param>
		public void Add ( MenuItems menus )
		{
			if ( menus == null )
			{
				throw new Exception ( "MenuItems Object is null" );
			}
			this.List.Add ( menus );
		}

		/// <summary>
		/// Remove menu
		/// </summary>
		/// <param name="menus"></param>
		public void Remove ( MenuItems menus )
		{
			if ( menus == null )
			{
				throw new Exception ( "MenuItems Object is null" );
			}
			this.List.Remove ( menus );
		}

		/// <summary>
		/// Get the menu by index
		/// </summary>
		public MenuItems this [ int index ]
		{
			get 
			{
				return ( MenuItems ) this.List [ index ]; 
			}
			set 
			{
				this.List [ index ] = value; 
			}
		}

		#endregion

		#region Events

		/// <summary>
		/// Do the action
		/// </summary>
		internal void DoAction()
		{
			if(Click!=null)
			{
				Click();
			}
		}

		/// <summary>
		/// Menu click handler
		/// </summary>
		public delegate void MenuClickHandler();

		/// <summary>
		/// Click event
		/// </summary>
		public event MenuClickHandler Click;

		#endregion

		#region Constructos
		
		/// <summary>
		/// Create a context menu with its properties
		/// </summary>
		/// <param name="strText">Text of the context menu</param>
		/// <param name="iLocation">Position of the context menu</param>
		/// <param name="bSeperator">Is the menu seperator</param>
		/// <param name="imgIcon">Icon to be shown</param>
		/// <param name="isIconTransparent">Is Icon Transparent</param>
		public MenuItems(string strText, int iLocation, bool bSeperator, Bitmap imgIcon, bool isIconTransparent)
		{
			if(!bSeperator)
			{
				this.Text = strText;
				this.Location = iLocation;
				this.Icon = imgIcon;
				this.IsIconTransparent = isIconTransparent;
			}
			else
			{
				this.IsSeperator = bSeperator;
			}
		}

		/// <summary>
		/// Create a context menu with its properties, it doesn't have an icon
		/// </summary>
		/// <param name="strText">Text of the context menu</param>
		/// <param name="iLocation">Position of the context menu</param>
		/// <param name="bSeperator">Is the menu seperator</param>
		public MenuItems(string strText, int iLocation, bool bSeperator)
		{
			if(!bSeperator)
			{
				this.Text = strText;
				this.Location = iLocation;
			}
			else
			{
				this.IsSeperator = bSeperator;
			}
		}

		internal MenuItems()
		{
		}

		#endregion

	}
}
